package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.config.result.ResultCode;
import com.future.hair.server.entity.*;
import com.future.hair.server.custom.Vo.UserInfoVo;
import com.future.hair.server.mapper.*;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("//hair/mall/user")
@Api(tags = "mall/user店员")
public class MallUserController {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private UserInfoMapper infoMapper;

    @Autowired
    private MallMapper mallMapper;

    @Autowired
    private BarberAppraiseMapper barberAppraiseMapper;

    @Autowired
    private WorksMapper worksMapper;


    @ApiOperation(value = "查看店员列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<UserInfoVo>> list(@RequestBody PageModel data, @RequestHeader String token) {
        List<UserInfoVo> list = new ArrayList<UserInfoVo>();
        if (JwtHelper.getByOpenId(token)==4444){
            QueryWrapper<UserInfo> infoWrapper = new QueryWrapper<UserInfo>();
            infoWrapper.eq("mall_id",JwtHelper.getByMallId(token));
            List<UserInfo> userInfoList = infoMapper.selectPage(new Page<>(data.getCurrent(), data.getSize()), infoWrapper).getRecords();
            for (UserInfo info :
                    userInfoList) {
                list.add(new HomeController().getUserInfoVo(info));
            }
            return CommonResult.data(list);
        }else {
            List<UserInfo> userInfoList = infoMapper.selectPage(new Page<>(data.getCurrent(), data.getSize()), null).getRecords();
            for (UserInfo info :
                    userInfoList) {
                list.add(getUserInfoVo(info));
            }
            return CommonResult.data(list);
        }
    }

    @ApiOperation(value = "查看店员详情")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<UserInfoVo> UserInfoUserVoId(@RequestBody UserInfo data, @RequestHeader String token) {
        return CommonResult.data(getUserInfoVo(infoMapper.selectById(data.getId())));
    }

    @ApiOperation(value = "添加店员")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Integer> install(@RequestBody UserInfo data ,@RequestHeader String token) {
        if (JwtHelper.getByOpenId(token)==4444){
            if (JwtHelper.getByMallId(token)==data.getMallId()){
                data.setMallId(JwtHelper.getByMallId(token));
            }else{
                return CommonResult.failed("不能添加其他店铺店员");
            }
        }
        if (null==data.getMobile() || null==data.getMallId()){
            return CommonResult.failed("参数不能为空");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("mobile", data.getMobile());
        User user = mapper.selectOne(wrapper);
        // 判断店员是否存在
        if (null != user){
            QueryWrapper<UserInfo> infoWrapper = new QueryWrapper<UserInfo>();
            wrapper.eq("user_id", user.getId());
            UserInfo info = infoMapper.selectOne(infoWrapper);
            if (null != infoMapper.selectOne(infoWrapper)){
                return CommonResult.failed("店员已存在");
            }

            data.setUserId(user.getId());
            data.setCreateTime(null);
            data.setUpdataTime(null);
            // 添加店员的info信息
            Integer result = infoMapper.insert(data);
            if (result == 1){
                return CommonResult.data(result);
            }else {
                return CommonResult.failed("创建失败");
            }
        }else {
            return CommonResult.failed(ResultCode.USER_NOT_FOUNT);
        }
    }

    @ApiOperation(value = "修改店员")
    @RequestMapping(value = "/upData", method = RequestMethod.POST)
    public CommonResult<Integer> upData(@RequestBody UserInfo data, @RequestHeader String token) {
        // 判断店员信息是否存在
        if (null != infoMapper.selectById(data.getId())){
            if (4444==JwtHelper.getByOpenId(token)){
                data.setMallId(null);
            }
            data.setUserId(null);
            data.setCreateTime(null);
            data.setUpdataTime(null);
            // 更新店员的info信息
            Integer result = infoMapper.updateById(data);
            if (result == 1){
                return CommonResult.data(result);
            }else {
                return CommonResult.failed("修改失败");
            }
        }else {
            return CommonResult.failed(ResultCode.USER_NOT_FOUNT);
        }
    }

    @ApiOperation(value = "删除店员")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<Integer> delete(@RequestBody UserInfo data, @RequestHeader String token) {
        UserInfo info = infoMapper.selectById(data.getId());
        // 判断店员信息是否存在
        if (null != info){
            if (4444==JwtHelper.getByOpenId(token)){
                if (JwtHelper.getByMallId(token)!=data.getMallId()){
                    return CommonResult.failed("不能删除其他店铺店员");
                }
            }
            // 删除店员的info信息
            Integer result = infoMapper.deleteById(info.getId());
            if (result == 1){
                return CommonResult.data(result);
            }else {
                return CommonResult.failed("删除失败");
            }
        }else {
            return CommonResult.failed(ResultCode.USER_NOT_FOUNT);
        }
    }



    public UserInfoVo getUserInfoVo(UserInfo data){
        if (null==data){
            return null;
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUserInfo(data);
//        userInfoVo.setUser(mapper.selectById(userInfo.getUserId()));
        userInfoVo.setMall(mallMapper.selectById(data.getMallId()));
        QueryWrapper<BarberAppraise> barberAppraiseWrapper = new QueryWrapper<BarberAppraise>();
        barberAppraiseWrapper.eq("barber_id", data.getId());
        userInfoVo.setAppraiseCount(barberAppraiseMapper.selectCount(barberAppraiseWrapper));
        barberAppraiseWrapper.eq("type", 1);
        userInfoVo.setGoodAppraiseCount(barberAppraiseMapper.selectCount(barberAppraiseWrapper));
        QueryWrapper<Works> worksWrapper = new QueryWrapper<Works>();
        worksWrapper.eq("user_info_id",data.getId());
        List<Works> worksList = worksMapper.selectPage(new Page<>(1, 10), worksWrapper).getRecords();
        userInfoVo.setImgList(worksList.stream().map(Works::getCover).collect(Collectors.toList()));
        return userInfoVo;
    }
}
